package com.hmall.controller;

import com.hdmall.business.common.common.ServerResponse;
import com.hmall.mapper.SysCustomerMapper;
import com.hmall.model.SysCustomer;
import com.hmall.service.*;
import com.hmall.vo.TestCustomerInformation;
import com.itextpdf.text.DocumentException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author zhoutao
 */
@RestController
@RequestMapping("/task/")
@CrossOrigin(origins = "http://localhost:8088", maxAge = 3600)
@Api(value = "定时任务API", description = "定时任务API说明")
public class TaskController {

    private final CommentService iCommentService;

    private final ProductClassService iProductClassService;

    private final MallProductPlanService mallProductPlanService;

    private final UserService userService;

    private final TaskService taskService;

    private final SysCustomerMapper sysCustomerMapper;

    @Autowired
    public TaskController(CommentService iCommentService, ProductClassService iProductClassService, MallProductPlanService mallProductPlanService, UserService userService, TaskService taskService, SysCustomerMapper sysCustomerMapper) {
        this.iCommentService = iCommentService;
        this.iProductClassService = iProductClassService;
        this.mallProductPlanService = mallProductPlanService;
        this.userService = userService;
        this.taskService = taskService;
        this.sysCustomerMapper = sysCustomerMapper;
    }

    @GetMapping("salesVolumeTask")
    @ApiOperation(value = "销量刷新工作任务")
    public ServerResponse salesVolumeTask(){

        return iProductClassService.updateSalesVolume();
    }


    /**
     * @Date 2018/11/9 10:43
     * @Param []
     * @return com.hmall.common.ServerResponse
     * @Description // 后台定时任务更新产品评分
     */
    @PostMapping("updateAveGrade")
    @ApiOperation(value = "定时任务更新产品评分")
    public ServerResponse updateAveGrade(){

        return iCommentService.updateAveGrade();
    }

    /**
     * @Date 2018/11/9 10:43
     * @Param []
     * @return com.hmall.common.ServerResponse
     * @Description //
     * 后台定时任务更新交易计划为审核中
     *    规则：1.24.00:00:00 的时候更新所有未审核的订单为审核中 0 --> 1
     *    2018/9/12 改版为可以自定义
     */
    @GetMapping("updatePlanToDo")
    @ApiOperation(value = "定时任务未审核改为审核中")
    public ServerResponse updatePlanToDo(){

        return  mallProductPlanService.updatePlanToDo();
    }


    /**
     * @Date 2018/11/2 15:00
     * @Param []
     * @return com.hmall.common.ServerResponse
     * @Description // 上个月没有订单产品的也变成已失效  默认前两个月的变成已失效
     * 前两个月以前的计划状态都变成已失效 4
     */
    @GetMapping("updatePlanToInvalid")
    @ApiOperation(value = "定时任务审核通过的改为审核失效")
    public ServerResponse updatePlanToInvalid(){

        return mallProductPlanService.updatePlanToInvalid();
    }

    /**
     * @Date 2018/11/9 10:43
     * @Param []
     * @return com.hmall.common.ServerResponse
     * @Description // 定时任务更改客户类型
     */
    @GetMapping("updateCustomerType")
    @ApiOperation(value = "定时任务更改客户类型")
    public ServerResponse updateCusotmerType(){

        return userService.updateCustomerType();
    }


    @GetMapping("updatePaymentStatement")
    @ApiOperation(value = "定时任务插入欠款")
    public ServerResponse updatePaymentStatement() throws InterruptedException {
        //先拉出客户列表
        List<SysCustomer> sysCustomerList = sysCustomerMapper.selectAll();
        return taskService.updatePaymentStatement(sysCustomerList);
    }

    @GetMapping("createSendImg")
    @ApiOperation(value = "创建发货单，月前")
    public ServerResponse createSendImg() throws FileNotFoundException, DocumentException {

        List<SysCustomer> sysCustomerList = sysCustomerMapper.selectAll();
        return taskService.createImg(sysCustomerList);
    }

    @GetMapping("createSendImgNow")
    @ApiOperation(value = "创建发货单，月中")
    public ServerResponse createSendImgNow() throws FileNotFoundException, DocumentException {

        List<SysCustomer> sysCustomerList = sysCustomerMapper.selectAll();
        return taskService.createImgNow(sysCustomerList);
    }


    @GetMapping("testCreateSendImgNow")
    @ApiOperation(value = "创建发货单，月中,测试")
    public ServerResponse testCreateSendImgNow(Integer customerId) throws FileNotFoundException, DocumentException {

        List<SysCustomer> sysCustomerList = new ArrayList<>();
        sysCustomerList.add(sysCustomerMapper.selectByPrimaryKey(customerId));
        return taskService.createImgNow(sysCustomerList);
    }

    @GetMapping("changePlanStatus")
    @ApiOperation(value = "更改计划状态", notes = "测试用")
    public ServerResponse changePlanStatus(Long planNo, Integer status){

        return taskService.changePlanStatus(planNo, status);
    }


    @GetMapping("testGetMoreInformationAboutCustomer")
    @ApiOperation(value = "得到这个公司有关的办事处，跟单员的信息", notes = "测试用")
    public ServerResponse<TestCustomerInformation> testGetMoreInformationAboutCustomer(String customerName){

        return taskService.testGetMoreInformationAboutCustomer(customerName);

    }

}
